package com.small.xx.ai.spring.toolcall.component.weather.method;

import cn.hutool.json.JSONUtil;
import com.small.xx.ai.spring.toolcall.component.weather.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class WeatherTools {

    private static final Logger logger = LoggerFactory.getLogger(WeatherTools.class);

    public WeatherTools() {
    }

    @Tool(description = "使用api.weather获取天气信息。")
    public Response getWeatherServiceMethod(@ToolParam(description = "城市名称") String city,
        @ToolParam(description = "天气预报的天数。值范围为1到2") int days) {
        System.out.println("WeatherTools.getWeatherServiceMethod");
        System.out.println("city = " + city + ", days = " + days);
        if (!StringUtils.hasText(city)) {
            logger.error("Invalid request: city is required.");
            return null;
        }
        List<Map> list = JSONUtil.toList(data(), Map.class);
        List<Map<String, Object>> list2 =
            list.stream().map(v -> JSONUtil.parseObj(JSONUtil.toJsonStr(v))).collect(Collectors.toList());
        return new Response(city, list2.getFirst(), list2);
    }

    private String data() {
        return "[{  " + "   \"date\": \"1\",  " + "   \"high\": \"高温 2℃\",  " + "   \"low\": \"低温 -3℃\",  " + "   \"ymd\": \"2019-12-16\",  " + "   \"week\": \"星期一\",  " + "   \"sunrise\": \"07:28\",  " + "   \"sunset\": \"16:50\",  " + "   \"aqi\": 97,  " + "   \"fx\": \"西风\",  " + "   \"fl\": \"<3级\",  " + "   \"type\": \"小雪\",  " + "   \"notice\": \"小雪虽美，赏雪别着凉\"  " + "  }," + " {  " + "   \"date\": \"2\",  " + "   \"high\": \"高温 5℃\",  " + "   \"low\": \"低温 -7℃\",  " + "   \"ymd\": \"2019-12-17\",  " + "   \"week\": \"星期二\",  " + "   \"sunrise\": \"07:29\",  " + "   \"sunset\": \"16:51\",  " + "   \"aqi\": 30,  " + "   \"fx\": \"北风\",  " + "   \"fl\": \"3-4级\",  " + "   \"type\": \"晴\",  " + "   \"notice\": \"愿你拥有比阳光明媚的心情\" }" + "]";
    }



}
